#include "hal_temp.h"
#include "hal_configer.h"
#include "hal_driver.h"
#include "math.h"
#include "common.h"
#include "cfg_drive.h"
#include "hal_tlv.h"
#include "sys_log.h"

float NTCData = 0;
float tempW = 0;
float tempVD = 0;
float tempV = 0;

float tempVolVal = 0;

float pdVol = 0;

float pdTempVal = 0;

float tempCurVal = 0;

extern TempCurrentParaSt g_TempCurrentRealValue;

void hal_TempInit()
{
    //hal_TemptDirectChange(g_TempCurrentRealValue.Temperature_Chn_1, LASER_NUMBER_CHAN_1);
	hal_TemptDirectChange(25, LASER_NUMBER_CHAN_1);

    // 打开温控芯片1
    HAL_TempDrivePinEnable(LASER_NUMBER_CHAN_1);
    systick_delay_ms(5);

    //hal_TemptDirectChange(g_TempCurrentRealValue.Temperature_Chn_2, LASER_NUMBER_CHAN_2);
	hal_TemptDirectChange(25, LASER_NUMBER_CHAN_2);

    // 打开温控芯片2
    HAL_TempDrivePinEnable(LASER_NUMBER_CHAN_2);
    //systick_delay_ms(5);

    //systick_delay_s(TEMP_INIT_WAIT_TIME_S);
}

// 温度缓慢增加和下降
void hal_TemptSlowChange(float target_temperature, int lasernum, uint8_t AD7689INX)
{
    float NowTemp = HAL_AD7689SpiReadTempOrTempCtrCurReal(AD7689INX, AD7689_LASER_TEMP_REGTOT, lasernum);

    // 目前温度不在正常范围内
    if (!hal_LaserReadValueNormal(JUDGE_LASER_LIMIT_TYPE_TEMP, NowTemp, lasernum))
    {
        SLOG_I("laser number:%d hal_TemptSlowChange read NowTemp error: %f \t\r\n", lasernum, NowTemp);
        return;
    }

    // 限制设置的温度
    if (!hal_LaserReadValueNormal(JUDGE_LASER_LIMIT_TYPE_TEMP, target_temperature, lasernum))
    {
        SLOG_I("laser number:%d hal_TemptSlowChange read target_temperature error: %f \t\r\n", lasernum, target_temperature);
        return;
    }

    // 步长基准
    int StepTemp = TEMP_STEP_BASE;

    float temp = NowTemp;

    if (NowTemp > target_temperature)
        StepTemp *= -1;

    if (NowTemp != target_temperature)
    {
        // 逐步增加电流并模拟过程
        for (int step = 0; step < floor(fabs(target_temperature - NowTemp)); ++step)
        {

            if (StepTemp > 0)
            {
                if (temp > target_temperature)
                {
                    hal_TemptDirectChange(target_temperature, lasernum);
                    break;
                }
            }
            else
            {
                if (temp < target_temperature + 1)
                {
                    hal_TemptDirectChange(target_temperature, lasernum);
                    break;
                }
            }

            temp += StepTemp;

            hal_TemptDirectChange(temp, lasernum);
        }
    }
    else
    {
        hal_TemptDirectChange(target_temperature, lasernum);
    }
}

void hal_TemptDirectChange(float temp, int laserNumber)
{
    // 输出给DAC8830
    if (hal_LaserWriteValueNormal(JUDGE_LASER_LIMIT_TYPE_TEMP, temp, laserNumber))
    {
        HAL_DAC8830SpiWriteTempReal(fabs(temp), laserNumber);
        systick_delay_ms(MS1_NUM);
    }
}

uint16_t hal_DAC8830SpiTempTToReg(float temp_real)
{
    float Beta = 3976;
    float Rfb  = 24;
    float T0 = 25 + 273.15, R0 = 10;
    float R_NTC     = R0 * exp(Beta * (1 / (temp_real + 273.15) - 1 / T0));
    float U_DAC8830 = R_NTC * DAC8830_VREF / (10 + R_NTC);

    // fix byme 写一个字节，需要用uint16_t存储数据吗
    // 可能是写错了，两个字节也可以试一下！！！
    uint16_t RegRealData = U_DAC8830 * DAC8830_MAXCODE / DAC8830_VREF;

    return RegRealData;
}

float hal_ADC7689TemptCtrCurRegToI(uint16_t tempctrcur)
{
    float MAX1978_VREF = 1.5, R1 = 0.544;

    // 数字电压转换成模拟电压
		//
    float tempVol = (float)tempctrcur / AD7689_MAXCODE * AD7689_VREF;
		
		tempVolVal = tempVol;
	
    // 将模拟电压转换为温控电流
    float tempctrcurrentact = (tempVol - MAX1978_VREF) / R1;
	
		tempCurVal = tempctrcurrentact;

    return tempctrcurrentact;
}

float hal_ADC7689TempRegtoT(uint16_t temp_V)
{
    float Beta         = 3976;
    float MAX1978_VREF = 1.5;
    float T0 = 298.15, R0 = 10;
    float temp_write = 0;
    float R_NTC      = 0;
    float Temp_Val   = 0;
		tempVD = temp_V;
    temp_write = (float)temp_V / AD7689_MAXCODE * AD7689_VREF;
		tempW = temp_write;
    R_NTC      = temp_write / ((MAX1978_VREF - temp_write) / R0);
		NTCData = R_NTC;
    Temp_Val   = Beta * T0 / ((T0 * log(R_NTC / R0)) + Beta) - 273.15;
		tempV = Temp_Val;
    return Temp_Val;
}

//参数是否要修改
float hal_ADC7689PDPowerRegToI(uint16_t temp_V){
	
	//pd电阻300欧姆
	float MAX1978_VREF = 1.5;
	float R1 = 300;
	float tempVol = (float)temp_V / AD7689_MAXCODE * AD7689_VREF;
	float pdpower = (tempVol) / R1 * 1000;//转换成mA
	
	//浮点运算精度控制
	pdTempVal = tempVol / 0.3;
	
	pdVol = tempVol;
	
	if(pdpower < 0)
		return 0;
	else
		return pdpower;
}
